package cn.nlte.frame.gui.tabclose;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 * 本类用于管理所有的JTabbedPaneManager及新增的面板
 *
 * 采用全静态方法是用本类，无需实例化
 *
 * @author yetao
 */
public class JTabbedPaneManager {

    private JTabbedPane jtabpane = null;
    private ArrayList<JPanel> panels = null;
    private ArrayList<JTabbedPanelTab> tabs = null;
    private ArrayList<String> panelnames = null;
    private ArrayList<Boolean> visibles = null;
    private static HashMap<String, JTabbedPaneManager> jtabpaneMap = null;

    private JTabbedPaneManager(JTabbedPane jtabpane) {
        this.jtabpane = jtabpane;
        panels = new ArrayList();
        tabs = new ArrayList();
        panelnames = new ArrayList();
        visibles = new ArrayList();
    }

    public static JTabbedPaneManager newInstance(String name, JTabbedPane jtabpane) {
        if (jtabpaneMap == null) {
            jtabpaneMap = new HashMap();
        }
        JTabbedPaneManager jtpm = new JTabbedPaneManager(jtabpane);
        jtabpaneMap.put(name, jtpm);
        return jtpm;
    }

    /**
     * 设置所选择的选项卡颜色
     *
     * @param tabname
     * @param tittlename
     * @param color
     */
    public static void setIndexColor(String tabname, String tittlename, Color color) {
        JTabbedPaneManager current = getInstance(tabname);
        current.setSelectedTabColor(tittlename, color);
    }

    /**
     * 获取管理的选项卡组件
     *
     * @param name
     * @return
     */
    public static JTabbedPaneManager getInstance(String name) {
        if (jtabpaneMap == null) {
            return null;
        }
        return jtabpaneMap.get(name);
    }

    public boolean addTab(String panelname, JTabbedPanelTab tab, JPanel panel, boolean isVisible) {
        if (isVisible) {
            jtabpane.add(panelname, panel);
            jtabpane.setTabComponentAt(jtabpane.indexOfTab(panelname), tab);
        }
        panels.add(panel);
        tabs.add(tab);
        panelnames.add(panelname);
        visibles.add(isVisible);
        return true;
    }

    /**
     * 设置所选择的选项卡颜色
     *
     * @param tittlename
     * @param color
     */
    public void setSelectedTabColor(String tittlename, Color color) {
        int index = panelnames.indexOf(tittlename);
        if (index >= 0) {
            for (JTabbedPanelTab tab : tabs) {
                tab.setLabelColor(Color.BLACK);
            }
            tabs.get(index).setLabelColor(color);
        }
    }

    public void removeTab(String panelname) {
        int index = panelnames.indexOf(panelname);
        if (index < 0) {
            return;
        }
        jtabpane.remove(jtabpane.indexOfComponent(panels.get(index)));
        visibles.set(index, false);
    }

    public void showTab(String panelname) {
        int index = panelnames.indexOf(panelname);
        if (index < 0) {
            return;
        }
        if (visibles.get(index)) {
            jtabpane.setSelectedIndex(jtabpane.indexOfTab(panelnames.get(index)));
            return;
        }
        jtabpane.add(panelnames.get(index), panels.get(index));
        jtabpane.setTabComponentAt(jtabpane.indexOfTab(panelnames.get(index)), tabs.get(index));
        jtabpane.setSelectedIndex(jtabpane.indexOfTab(panelnames.get(index)));
        visibles.set(index, true);
    }

    /**
     * @return the jtabpaneMap
     */
    public static HashMap<String, JTabbedPaneManager> getJtabpaneMap() {
        return jtabpaneMap;
    }

}
